package com.lauor.test.create;

import com.lauor.smpedr.session.SqlSession;
import com.lauor.test.BaseSpringBootUser;
import com.lauor.test.entity.User;
import org.junit.Test;

import java.util.*;

/**
 * 正常创建user
 */
public class NormalCreateUser extends BaseSpringBootUser {
    //正常创建一条数据，id自增
    @Test
    public void createUser(){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        User user = this.buildUserMsg();
        try {
            int rows = sqlSession.insert(User.class, user);
            System.out.println( String.format("插入成功数量:%d", rows) );
            System.out.println( String.format("自增id是:%d", user.getId()) );
        } catch (Throwable throwable){
            throwable.printStackTrace();
        }
    }
    //正常创建4条数据，id自增
    @Test
    public void batchCreateUser(){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<User> userList = new ArrayList<>(4);
        for (int i = 0; i < 4; i++) {
            User user = this.buildUserMsg();
            userList.add(user);
        }
        try {
            int rows = sqlSession.batchInsert(User.class, userList);
            System.out.println( String.format("插入成功数量:%d", rows) );
        } catch (Throwable throwable){
            throwable.printStackTrace();
        }
    }
}
